package com.example.smartcity_40.Outpatient;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.example.smartcity_40.Outpatient.apter.VisitorApter;
import com.example.smartcity_40.Outpatient.bean.VisitorBean;
import com.example.smartcity_40.R;
import com.example.smartcity_40.tool.Tool;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class RegisterActivity extends AppCompatActivity {

    private RegisterActivity context;
    private RecyclerView visitorlist;
    private Tool tool;
    private String TAG = "TAG";
    private VisitorApter visitorApter;
    private int ADDVISITORID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        context = this;
        initview();
        visitorlistmethod();
    }

    @Override
    protected void onResume() {
        super.onResume();
        visitorlistmethod();
    }

    private void visitorlistmethod() {
        Log.v(TAG, "TAG" + tool.get("token"));
        tool.send("/prod-api/api/hospital/patient/list",
                Tool.GET, null, true,
                VisitorBean.class,
                new Function1<VisitorBean, Unit>() {
                    @Override
                    public Unit invoke(VisitorBean visitorBean) {
                        visitorlist.setLayoutManager(new LinearLayoutManager(context));
                        visitorApter.setDate(visitorBean.getRows());
                        visitorlist.setAdapter(visitorApter);
                        return null;
                    }
                });
    }

    private void initview() {
        ImageView back = findViewById(R.id.back);
        ImageView addVisitor = findViewById(R.id.addVisitor);
        visitorlist = findViewById(R.id.Visitorlist);
        tool = new Tool(context);
        visitorApter = new VisitorApter(context);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });

        addVisitor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AddvisitorActivity.class);
                context.startActivityForResult(intent, ADDVISITORID);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADDVISITORID && resultCode == RESULT_OK) {
            //刷新就诊人
            visitorlistmethod();
        }
    }
}